﻿<%@ Page Language="C#" MasterPageFile="~/Pages/Base/ListItemBase.master" CodeFile="~/Pages/Base/ListItemBase.cs" Inherits="ListItemBasePage" %>
<asp:Content ID="content" runat="server" ContentPlaceHolderID="footContent">
<asp:Label ID="nfLabel" runat="server" Text="年份"></asp:Label>
<asp:TextBox ID="nfTextBox" runat="server" MaxLength="4" Width="100px" ></asp:TextBox>
<asp:Button ID="goButton" runat="server" Text="Go" />
<asp:Button ID="addPMButton"
    runat="server" Text="新增篇名" style="margin-left: 37px" />
 </asp:Content>
<asp:Content ID="message" runat="server" ContentPlaceHolderID="messageContent">
<asp:Label ID="errorLabel" runat="server" ForeColor="Red">
</asp:Label>
</asp:Content>
<script runat="server">
    
    /// <summary>
    /// 数据库交互对象
    /// </summary>

    private StateGrid.HYBZ.HYBZ_DAO dao = new StateGrid.HYBZ.HYBZ_DAO();

    private IList<StateGrid.HYBZ.HYBZ_PM> data;
    /// <summary>
    /// 分类编码
    /// </summary>
    private String flbm;
    /// <summary>
    /// 年份
    /// </summary>
    private String nf;

    protected override void DoInit()
    {        
        //分类编码
        this.flbm = Request["catalog"];
        //分类年份
        this.nf = Request["nf"];
        if (null != nf && !"".Equals(nf))
        {
            //this.nfTextBox.Text = nf;
        }
        //添加年份和期号搜索
        this.goButton.Click += new EventHandler(goButton_Click);
        //添加指定年份的篇名
        this.addPMButton.Click += new EventHandler(addPMButton_Click);

        this.SetAddPMVisible();
    }

    void addPMButton_Click(object sender, EventArgs e)
    {
        if (!ValidateNF())
        {
            return;
        }
        Response.Redirect(StateGrid.GlobalVariables.GlobalHYBZ.ADDPM_PAGE + "?flbm=" + this.flbm + "&nf=" + this.nfTextBox.Text);
    }

    void goButton_Click(object sender, EventArgs e)
    {
        if (!ValidateNF())
        {
            return;
        }
        Response.Redirect(StateGrid.GlobalVariables.GlobalHYBZ.LISTPM_PAGE + "?catalog=" + this.flbm + "&nf=" + this.nfTextBox.Text );
    }

    protected override void SetItemFields()
    {
        RegisterKeyField("bzwh");
        
        AddBoudField("标准文号", "bzwh", 0, 15);
        AddBoudField("分类编码", "flbm",0, 5);
        AddBoudField("文件名", "wjm", 20, 10);
        AddBoudField("中文名称", "zwmc", 30, 35);
        AddBoudField("作废", "zf", 0, 5);

        AddHyperLinkField("代替标准", "dtbz", StateGrid.GlobalVariables.GlobalHYBZ.EDITPM_PAGE + "?bzwh={0}", new string[]{"dtbz"});
        AddHyperLinkField("被代替标准", "bdtbz", StateGrid.GlobalVariables.GlobalHYBZ.EDITPM_PAGE + "?bzwh={0}", new string[]{"bdtbz"});
        

        //AddBoudField("代替标准", "dtbz", 0, 15);
        //AddBoudField("被代替标准", "bdtbz", 0, 15);
        AddHyperLinkField("替代",
                          StateGrid.GlobalVariables.GlobalHYBZ.ADDPM_PAGE + "?bzwh={0}",
                          new string[] {"bzwh"}); 
        AddHyperLinkField("修改",
                          StateGrid.GlobalVariables.GlobalHYBZ.EDITPM_PAGE + "?bzwh={0}",
                          new string[] {"bzwh"}); 
    }

    protected override int GetItemCount()
    {
        return dao.GetCountSomeHYBZ_PM(this.flbm, this.nf);
    }

    protected override object GetItemSource(int begin, int end)
    {
        data = dao.GetSomeHYBZ_PM(this.flbm, this.nf, begin, end);
        return data;
    }

    protected override bool OnDelete(DataKey dataKey, out string message)
    {
        int ret = dao.DeleteHYBZ_PM(dataKey.Value as string,((StateGrid.User.User)Session["login"]).name);

        if (ret>0)
        {
            message = "删除成功";
            return true;
        }

        message = "删除失败";
        return false;
    }

    /// <summary>
    /// 验证年份
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateNF()
    {
        //清空错误信息
        this.errorLabel.Text = "";
        
        Boolean flag = true;
        int year = 0;

        if (null == this.nfTextBox.Text || this.nfTextBox.Text.Trim().Length != 4)
        {
            flag = false;
        }
        else
        {
            this.nfTextBox.Text = this.nfTextBox.Text.Trim();
            try
            {
                year = Int32.Parse(this.nfTextBox.Text);
            }
            catch (Exception)
            {
                flag = false;
            }
            if (year < 1000)
            {
                flag = false;
            }
        }
        if (!flag)
        {
            this.errorLabel.Text = "错误：输入的年份有误，年份区间1000~9999";
        }
        return flag;
    }

    //显示删除按钮
    protected override Boolean HasDeleteField()
    {
        return true;
    }

    /// <summary>
    /// 设置添加篇名是否可见
    /// </summary>
    public void SetAddPMVisible()
    {
        if (null == this.flbm || this.flbm.Length < 4)
        {
            this.nfLabel.Visible = false;
            this.nfTextBox.Visible = false;
            this.addPMButton.Visible = false;
            this.goButton.Visible = false;
        }
        else
        {
            this.nfLabel.Visible = true;
            this.nfTextBox.Visible = true;
            this.addPMButton.Visible = true;
            this.goButton.Visible = true;
        }
    }
</script>
